package com.game.server.db.dao;

import com.game.majiang.config.DataManager;
import com.game.majiang.config.LogManager;
import com.game.server.db.Parameter;
import com.game.server.db.ParameterType;
import com.game.server.db.SqlHelper;
import com.game.server.db.entity.UserInfoData;
import com.game.server.db.entity.XyClubInfo;
import com.game.server.db.entity.XyClubMemberInfo;
import com.game.server.util.GetBeanFromResultUtil;
import com.game.server.util.IdWorker;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.sql.rowset.CachedRowSet;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class XyClubMemberInfoDao {

    public static List<XyClubMemberInfo> selectAll() {//查询所有俱乐部用户数据
        List<XyClubMemberInfo> listClubMenber = new ArrayList<>();
        String sql = "SELECT * FROM xy_club_member_info";
        CachedRowSet resultSet = SqlHelper.getResultSet(sql);
        try {
            while (resultSet.next()) {
                XyClubMemberInfo info = GetBeanFromResultUtil.getBean(resultSet, XyClubMemberInfo.class, null);
                listClubMenber.add(info);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return listClubMenber;
    }

    //添加
    public static long insertInto(XyClubMemberInfo info) {
        long id = DataManager.getInstance().getWorker().nextId();
        LogManager.getInstance().Log("添加俱乐部成员ID:"+String.valueOf(id));
        //加入创建时间
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date1=formatter.format(date);
        info.setCreatedate(Timestamp.valueOf(date1));

        String sql = "INSERT INTO xy_club_member_info(id,cid,uid,isManager,pid,createdate) VALUES (?,?,?,?,?,?)";
        Parameter p1 = new Parameter(Types.BIGINT, id, ParameterType.Input);
        Parameter p2 = new Parameter(Types.INTEGER, info.getCid(), ParameterType.Input);
        Parameter p3 = new Parameter(Types.INTEGER, info.getUid(), ParameterType.Input);
        Parameter p4 = new Parameter(Types.INTEGER, info.getIsManager(), ParameterType.Input);
        Parameter p5 = new Parameter(Types.INTEGER, info.getPid(), ParameterType.Input);
        Parameter p6 = new Parameter(Types.TIMESTAMP, info.getCreatedate(), ParameterType.Input);
        SqlHelper.execute(sql, p1, p2, p3, p4, p5,p6);
        return id;
    }

    /**
     * 根据ID查询
     *
     * @param id
     * @return
     */
    public static XyClubMemberInfo findById(long id) {
        XyClubMemberInfo clubMemberInfo = new XyClubMemberInfo();
        String cmdText = "SELECT * FROM xy_club_member_info WHERE id = ?";
        Parameter p1 = new Parameter(Types.BIGINT, id, ParameterType.Input);
        CachedRowSet resultSet = SqlHelper.getResultSet(cmdText, p1);
        try {
            if (resultSet.next()) {
                //将查询到的数据转为对象
                clubMemberInfo = GetBeanFromResultUtil.getBean(resultSet, XyClubMemberInfo.class, null);
            }
        } catch (SQLException e) {
            LogManager.getInstance().errorLog(e);
        }
        return clubMemberInfo;
    }

    /**
     * 修稿俱乐部用户合伙人状态
     *
     * @param cid         俱乐部ID
     * @param uid         用户ID
     * @param isAccessory 是否为合伙人0 不是 1是
     */
    public static int updeteIsAccessory(int cid, int uid, int isAccessory) {
        String sql = "UPDATE xy_club_member_info SET isAccessory=? WHERE cid = ? and uid=?";
        Parameter p1 = new Parameter(Types.INTEGER, isAccessory, ParameterType.Input);
        Parameter p2 = new Parameter(Types.INTEGER, cid, ParameterType.Input);
        Parameter p3 = new Parameter(Types.INTEGER, uid, ParameterType.Input);
        return SqlHelper.execute(sql, p1, p2, p3);
    }

    /**
     * 修稿俱乐部用户合伙人ID
     *
     * @param cid       俱乐部ID
     * @param uid       用户ID
     * @param partnerId 成为合伙人的ID
     */
    public static int updetePartnerId(int cid, int uid, int partnerId) {
        String sql = "UPDATE xy_club_member_info SET partner_id=? WHERE cid = ? and uid=?";
        Parameter p1 = new Parameter(Types.INTEGER, partnerId, ParameterType.Input);
        Parameter p2 = new Parameter(Types.INTEGER, cid, ParameterType.Input);
        Parameter p3 = new Parameter(Types.INTEGER, uid, ParameterType.Input);
        return SqlHelper.execute(sql, p1, p2, p3);
    }


    /**
     * 修改俱乐部用户身份
     *
     * @param cid       俱乐部ID
     * @param uid       用户ID
     * @param isManager 1创建者 2.管理员 3.普通成员
     */
    public static int updateIsManager(int cid, int uid, int isManager) {
        String sql = "UPDATE xy_club_member_info SET isManager=? WHERE cid = ? and uid=?";
        Parameter p1 = new Parameter(Types.INTEGER, isManager, ParameterType.Input);
        Parameter p2 = new Parameter(Types.INTEGER, cid, ParameterType.Input);
        Parameter p3 = new Parameter(Types.INTEGER, uid, ParameterType.Input);
        return SqlHelper.execute(sql, p1, p2, p3);
    }

    /**
     * 删除俱乐部用户
     *
     * @param id   ID
     * @param type 用户ID
     * @return
     */
    public static int deleteClubMember(long id, int type) {
        String sql = "UPDATE xy_club_member_info SET del_type=? WHERE id = ?";
        Parameter p1 = new Parameter(Types.INTEGER, type, ParameterType.Input);
        Parameter p2 = new Parameter(Types.BIGINT, id, ParameterType.Input);
        return SqlHelper.execute(sql, p1, p2);
    }

    /**
     * 修改用户的信誉额度
     *
     * @param toClubMember
     */
    public static void updateXingyuvalue(XyClubMemberInfo toClubMember) {
        String sql = "UPDATE xy_club_member_info SET xingyuvalue=? WHERE id = ?";
        Parameter p1 = new Parameter(Types.INTEGER, toClubMember.getXingyuvalue(), ParameterType.Input);
        Parameter p2 = new Parameter(Types.INTEGER, toClubMember.getId(), ParameterType.Input);
        SqlHelper.execute(sql, p1, p2);
    }

    /**
     * 通过ID获取合伙人ID是否已经存在
     *
     * @param xyclubid
     * @return
     */
    public static int findPartnerIdCount(int xyclubid) {
        int count = 0;
        String sql = "select count(id) from xy_club_member_info where partner_id=" + xyclubid;
        Object scalar = SqlHelper.execScalar(sql);
        count = (int) ((Math.random() * 9 + 1) * 100000);
        return count;
    }

    /**
     * 通过合伙人ID获取俱乐部ID
     *
     * @param xyclubid
     * @return
     */
    public static int findPartnerIdBackCid(int xyclubid) {
        int cid = 0;
        String sql = "select cid from xy_club_member_info where partner_id=" + xyclubid + " and isAccessory=1";
        Object scalar = SqlHelper.execScalar(sql);
        if (scalar != null) {
            cid = (int) Double.parseDouble(scalar.toString());
        }
        return cid;
    }

    public static JSONObject findJuniorMemberPage(int limit, Integer page, Integer clubid, Integer uid) {
        JSONObject jsonObject = new JSONObject();
        JSONArray array = new JSONArray();
        String countSql = "select count(cm.id) from xy_club_member_info AS cm where cm.cid="+clubid+" and cm.pid="+uid+" and cm.del_type=0 and cm.isAccessory=0";
        String sql = "SELECT cm.uid , ( SELECT CONCAT(IFNULL(SUM(IF(cd.creationdate = curdate() AND cd.cid = cm.cid AND cd.uid = cm.uid AND cd.pid = cm.pid, cd.day_contribute, 0)), 0), ',', IFNULL(SUM(IF(cd.creationdate = curdate() AND cd.cid = cm.cid AND cd.uid = cm.uid AND cd.pid = cm.pid, cd.day_shoushu, 0)), 0), ',', IFNULL(SUM(IF(cd.cid = cm.cid AND cd.uid = cm.uid AND cd.pid = cm.pid, cd.day_contribute, 0)), 0)) FROM xy_club_user_daydata cd ) AS daydata FROM xy_club_member_info cm WHERE cm.cid = "+clubid+" AND cm.pid = "+uid+" AND cm.del_type = 0 AND cm.isAccessory = 0 LIMIT ?,"+limit;
        Object scalar = SqlHelper.execScalar(countSql);
        int sumCount = Integer.parseInt(scalar.toString());
        int sumPage = (int) Math.ceil((double) sumCount/limit);
        page = page<1?1:page>sumPage?sumPage:page;
        page = page<1?1:page;
        Parameter p1 = new Parameter(Types.INTEGER, ((page-1)*limit), ParameterType.Input);
        CachedRowSet resultSet = SqlHelper.getResultSet(sql,p1);
        jsonObject.put("page",page);
        jsonObject.put("allpage",sumPage);
        jsonObject.put("allPlayercount",sumCount);
        try {
            while (resultSet.next()) {
                JSONObject jsonData = new JSONObject();
                int userid = resultSet.getInt(1);
                String strData = resultSet.getString(2);
                jsonData.put("uid",userid);
                jsonData.put("strData",strData);
                array.add(jsonData);
            }
        } catch (SQLException e) {
            LogManager.getInstance().errorLog(e);
        }
        jsonObject.put("array",array);
        return jsonObject;
    }

    public static int updateBonusShare(long id, Integer proportion) {
        String sql = "UPDATE xy_club_member_info SET bonus_share=? WHERE id = ?";
        Parameter p1 = new Parameter(Types.INTEGER, proportion, ParameterType.Input);
        Parameter p2 = new Parameter(Types.BIGINT, id, ParameterType.Input);
        return SqlHelper.execute(sql, p1, p2);
    }

    public static int updateBalanceBonus(long id, int value) {
        String sql = "UPDATE xy_club_member_info SET balance_bonus=balance_bonus+? WHERE id = ?";
        Parameter p1 = new Parameter(Types.INTEGER, value, ParameterType.Input);
        Parameter p2 = new Parameter(Types.BIGINT, id, ParameterType.Input);
        return SqlHelper.execute(sql, p1, p2);
    }

    public static int addAggregateBonus(long id, int value) {
        String sql = "UPDATE xy_club_member_info SET aggregate_bonus=aggregate_bonus+? WHERE id = ?";
        Parameter p1 = new Parameter(Types.INTEGER, value, ParameterType.Input);
        Parameter p2 = new Parameter(Types.BIGINT, id, ParameterType.Input);
        return SqlHelper.execute(sql, p1, p2);
    }

    public static int addXingyuvalue(long id, int value) {
        String sql = "UPDATE xy_club_member_info SET xingyuvalue=xingyuvalue+? WHERE id = ?";
        Parameter p1 = new Parameter(Types.INTEGER, value, ParameterType.Input);
        Parameter p2 = new Parameter(Types.BIGINT, id, ParameterType.Input);
        return SqlHelper.execute(sql, p1, p2);
    }
}
